﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.Utils;

namespace ProgramMain.Framework
{
    public partial class BaseListCtl : ProgramMain.Framework.BaseDetailCtl
    {
        public BaseListCtl()
        {
            InitializeComponent();
        }

        private string m_viewRowCaption = "";
        /// <summary>
        /// Заголовок выбранного объекта в списке
        /// </summary>
        public string ViewRowCaption
        {
            get { return m_viewRowCaption; }
            set
            {
                m_viewRowCaption = value;
            }
        }

        private Nullable<Int32> m_viewRowIDType = new Nullable<Int32>();
        /// <summary>
        /// Тип идентификатора выбранного объекта в списке
        /// </summary>
        public Nullable<Int32> ViewRowIDType
        {
            get { return m_viewRowIDType; }
            set
            {
                m_viewRowIDType = value;
            }
        }

        private Nullable<Int32> m_ViewRow_ID;
        /// <summary>
        /// Фильтр по объекту
        /// </summary>
        public Nullable<Int32> ViewRowID
        {
            get { return m_ViewRow_ID; }
            set
            {
                if (m_ViewRow_ID != value)
                {
                    m_ViewRow_ID = value;
                    ShowDetailEventArgs ed = new ShowDetailEventArgs(m_ViewRow_ID, (EnableEdit) ? ShowDetailModes.Edit : ShowDetailModes.ViewOnly, ViewRowCaption, ViewRowIDType);
                    OnViewRowIDChanged(ed);
                }
            }
        }

        /// <summary>
        /// Событие, срабатывает на изменение фильтра по объекту
        /// </summary>
        public event ShowDetailEventHandler ViewRowIDChanged;

        protected virtual void OnViewRowIDChanged(ShowDetailEventArgs e)
        {
            if (ViewRowIDChanged != null)
                ViewRowIDChanged(this, e);
        }

        private bool m_bAutoAcceptChanges = true;
        public bool AutoAcceptChanges
        {
            set
            {
                if (m_bAutoAcceptChanges != value)
                {
                    m_bAutoAcceptChanges = value;
                    OnAutoAcceptValueChanged(m_bAutoAcceptChanges);
                }
            }
            get
            {
                return m_bAutoAcceptChanges;
            }
        }

        protected virtual void OnAutoAcceptValueChanged(bool bAutoAcceptChanges)
        {
        }


        private bool m_bRefreshAfterChange = false;
        public bool RefreshAfterChange
        {
            set
            {
                m_bRefreshAfterChange = value;
            }
            get
            {
                return m_bRefreshAfterChange;
            }
        }

        protected bool m_DataSourceChanged = false;
        public bool DataSourceChanged
        {
            get
            {
                return m_DataSourceChanged;
            }
        }

        virtual public void RefreshControl()
        {
            if (m_bAutoAcceptChanges)
            {
                DBRead();
            }
        }

        protected virtual void Action_Add(object sender, EventArgs e)
        {
            if (dataSource != null)
            {
                if (!CheckSecurityEditAction())
                    return;

                this.dataSource.AddNew();

                DataRowView CurrentRowView = (DataRowView)this.dataSource.Current;
                CurrentRowView.Row["UserID"] = ProgramMain.Framework.BaseSecurity.UserID;

                PreCreateDetailDialog(BaseModalDetailCtl.ShowModeTypes.Add, CurrentRowView);

                BaseModalDetailCtl frm = CreateDetailDialog(BaseModalDetailCtl.ShowModeTypes.Add);

                DialogResult dRes = DialogResult.Cancel;
                if (frm != null)
                {
                    frm.dataSource = this.dataSource;
                    dRes = frm.ShowModal(BaseModalDetailCtl.ShowModeTypes.Add);
                }

                PostCreateDetailDialog(BaseModalDetailCtl.ShowModeTypes.Add, CurrentRowView.Row, dRes);

                if (dRes == DialogResult.OK)
                {
                    if (AutoAcceptChanges)
                    {
                        DBWrite();

                        if (RefreshAfterChange)
                            RefreshControl();
                    }

                    m_DataSourceChanged = true;

                    AfterChangeDetailDialog(BaseModalDetailCtl.ShowModeTypes.Add, CurrentRowView);
                }
            }
        }

        protected virtual void Action_Edit(object sender, EventArgs e)
        {
            if (this.dataSource != null && this.dataSource.Current != null)
            {
                DataRowView CurrentRowView = (DataRowView)this.dataSource.Current;
                CurrentRowView.Row["UserID"] = ProgramMain.Framework.BaseSecurity.UserID;

                bool isEditible = CheckSecurityEditAction(false);
                BaseModalDetailCtl.ShowModeTypes ShowMode = (isEditible) ? BaseModalDetailCtl.ShowModeTypes.Edit : BaseModalDetailCtl.ShowModeTypes.View;

                PreCreateDetailDialog(ShowMode, CurrentRowView);

                BaseModalDetailCtl frm = CreateDetailDialog(ShowMode);
                
                DialogResult dRes = DialogResult.Cancel;
                if (frm != null)
                {
                    frm.dataSource = this.dataSource;
                    dRes = frm.ShowModal(ShowMode);
                }

                PostCreateDetailDialog(ShowMode, CurrentRowView.Row, dRes);

                if (dRes == DialogResult.OK)
                {
                    
                    if (AutoAcceptChanges)
                    {
                        DBWrite();

                        if (RefreshAfterChange)
                            RefreshControl();
                    }

                    m_DataSourceChanged = true;

                    AfterChangeDetailDialog(ShowMode, CurrentRowView);
                }
            }
        }

        protected virtual void Action_Delete(object sender, EventArgs e)
        {
            if (this.dataSource != null && this.dataSource.Current != null)
            try
            {
                if (!CheckSecurityDeleteAction()) return;

                DataRowView CurrentRowView = (DataRowView)this.dataSource.Current;
                CurrentRowView.Row["UserID"] = ProgramMain.Framework.BaseSecurity.UserID;
                
                PreCreateDetailDialog(BaseModalDetailCtl.ShowModeTypes.Delete, CurrentRowView);

                BaseModalDetailCtl frm = CreateDetailDialog(BaseModalDetailCtl.ShowModeTypes.Delete);

                DataRow CurrentRow = null;

                DialogResult dRes = DialogResult.Cancel;
                if (frm != null)
                {
                    DataTable dt = CurrentRowView.Row.Table.Clone();
                    dt.ImportRow(CurrentRowView.Row);
                    CurrentRow = dt.Rows[0];
                    
                    frm.dataSource = this.dataSource;
                    dRes = frm.ShowModal(BaseModalDetailCtl.ShowModeTypes.Delete);
                }

                PostCreateDetailDialog(BaseModalDetailCtl.ShowModeTypes.Delete, CurrentRow, dRes);

                if (dRes == DialogResult.OK)
                {
                    if (AutoAcceptChanges)
                    {
                        DBWrite();

                        if (RefreshAfterChange)
                            RefreshControl();
                    }

                    m_DataSourceChanged = true;

                    AfterChangeDetailDialog(BaseModalDetailCtl.ShowModeTypes.Delete, null);
                }
                else
                {
                    CancelChangeDetailDialog(BaseModalDetailCtl.ShowModeTypes.Delete, null);
                }
            }

            catch{}
        }

        protected virtual void Action_Refresh(object sender, EventArgs e)
        {
            RefreshControl();
        }
        
        virtual protected BaseModalDetailCtl CreateDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode)
        {
            return null;
        }

        virtual protected void PreCreateDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode, DataRowView RowView)
        {
        }

        virtual protected void PostCreateDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode, DataRow Row, DialogResult dialogResult)
        {
        }

        virtual protected void AfterChangeDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode, DataRowView RowView)
        {
        }

        virtual protected void CancelChangeDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode, DataRowView RowView)
        {
        }
    }
}
